fix memory leak in mkshort from d7dfed6, 1/23/2005. (#1162)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Mon, 28 Aug 2023 23:21:28 +0000 (17:21 -0600)
committerGitHub <noreply@github.com>
Mon, 28 Aug 2023 23:21:28 +0000 (17:21 -0600)
defs.h
garmin.cc
mkshort.cc

diff --git a/defs.h b/defs.h
index ac1f8548991b75ee642d20de0c2c699c4a76c976..e024252814675417971c61131b822fd519508a4b 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -854,8 +854,8 @@ using ff_readposn = Waypoint* (*)(posn_status*);
 struct mkshort_handle_imp; // forward declare, definition in mkshort.cc
 using short_handle = mkshort_handle_imp*;
 
-char* mkshort(short_handle,  const char*, bool);
-QString mkshort(short_handle,  const QString&);
+QByteArray mkshort(short_handle, const char*, bool);
+QString mkshort(short_handle, const QString&);
 short_handle mkshort_new_handle();
 QString mkshort_from_wpt(short_handle h, const Waypoint* wpt);
 void mkshort_del_handle(short_handle* h);
index 97ccdc385184a3cd6920eec37aca54d7fc7b11de..8bf801052b559fb34308d8880f60c4c37ff83319 100644 (file)
--- a/garmin.cc
+++ b/garmin.cc
@@ -888,20 +888,16 @@ waypoint_prepare()
      * mkshort will do collision detection and namespace
      * cleaning
      */
-    char* ident = mkshort(mkshort_handle,
-                          global_opts.synthesize_shortnames ?
-                          str_from_unicode(src).constData() :
-                          str_from_unicode(wpt->shortname).constData(),
-                          false);
+    QByteArray ident = mkshort(mkshort_handle,
+                               global_opts.synthesize_shortnames ?
+                               str_from_unicode(src).constData() :
+                               str_from_unicode(wpt->shortname).constData(),
+                               false);
     /* Should not be a strcpy as 'ident' isn't really a C string,
      * but rather a garmin "fixed length" buffer that's padded
      * to the end with spaces.  So this is NOT (strlen+1).
      */
-    write_char_string(tx_waylist[i]->ident, ident, sizeof(tx_waylist[i]->ident));
-
-    if (global_opts.synthesize_shortnames) {
-      xfree(ident);
-    }
+    write_char_string(tx_waylist[i]->ident, ident.constData(), sizeof(tx_waylist[i]->ident));
 
     // If we were explicitly given a comment from GPX, use that.
     //  This logic really is horrible and needs to be untangled.
index d8945ecb9ba6d48944a94369882a8f5d1796ea62..ec78bffff76942a5a1bf6cca508c0e2467e59176 100644 (file)
@@ -353,7 +353,7 @@ setshort_mustuniq(short_handle h, int i)
   hdl->must_uniq = i;
 }
 
-char*
+QByteArray
 mkshort(short_handle h, const char* istring, bool is_utf8)
 {
   char* ostring;
@@ -556,18 +556,17 @@ mkshort(short_handle h, const char* istring, bool is_utf8)
   }
 
   if (hdl->must_uniq) {
-    return mkshort_add_to_list(hdl, ostring);
+    ostring = mkshort_add_to_list(hdl, ostring);
   }
-  return ostring;
+  QByteArray rval(ostring);
+  xfree(ostring);
+  return rval;
 }
 
 QString
 mkshort(short_handle h, const QString& istring)
 {
-  char* t =  mkshort(h, CSTR(istring), true);
-  QString r(t);
-  xfree(t);
-  return r;
+  return mkshort(h, CSTR(istring), true);
 }
 
 /*